package kxfang.com.common.core.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.fragment.app.Fragment;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

import kxfang.com.common.core.listener.BackPressListener;
import kxfang.com.common.core.listener.NavigationResult;

/**
 * 界面跳转工具类
 */
public class Navigate {

    NavigationActivity activity;
    NavigationBar bar;

    private Object[] args;
    private NavigationResult result;

    private volatile static Map<String, SoftReference<Navigate>> activityMap = new HashMap<>();
    private Navigate preNavigate;
    private String curTag;
    private String preTag;

    private BackPressListener listener;

    public Object[] getArgs() {
        return args;
    }

    public BackPressListener getListener() {
        return listener;
    }

    public void setListener(BackPressListener listener) {
        this.listener = listener;
    }

    /**
     * 跳转类
     *
     * @param curTag 当前activity的tag，
     * @param preTag 之前的activity的tag
     */
    Navigate(String curTag, String preTag) {
        this.curTag = curTag;
        this.preTag = preTag;
        activityMap.put(curTag, new SoftReference<Navigate>(this));
        // 如果前一个的tag不为空 则获取前一个的navigate
        if (!TextUtils.isEmpty(preTag)) {
            preNavigate = activityMap.get(preTag).get();
        }
    }

    /**
     * 根据tag获取Nav
     *
     * @param tag
     * @return
     */
    public static Navigate getInstance(String tag) {
        SoftReference<Navigate> navSoft = activityMap.get(tag);
        return navSoft.get();
    }

    public static Navigate getInstance(Fragment fragment) {
        return getInstance(fragment.getContext());
    }

    public static Navigate getInstance(Context context) {
        if (context instanceof Activity) {
            return getInstance((NavigationActivity) context);
        }
        return null;
    }

    public static Navigate getInstance(Activity activity) {
        if (activity instanceof NavigationActivity) {
            Navigate navigate = ((NavigationActivity) activity).getNavigate();
            return navigate;
        }

        new Throwable("Activity is not NavigationActivity");
        return null;
    }

    /**
     * 界面跳转（入栈）
     *
     * @param activity 当前activity
     * @param viewCls  要去的界面（可以是fragment或activity)
     * @param result   下个界面出栈时的回调函数
     * @param params   进入下个界面要传入的参数
     */
    private static void pushImpl(Activity activity, Class<?> viewCls, NavigationResult result, Object... params) {
        Intent intent = new Intent(activity, NavigationActivity.class);
        if (viewCls != null) {
            intent.putExtra("viewCls", viewCls.getName());
        }
        // 给每个activity设置一个tag
        String activityTag = "activity_tag" + System.currentTimeMillis() + Math.round(1000);

        intent.setAction(activityTag);

        String preTag = "";
        if (activity instanceof NavigationActivity) {
            // 获取当前activity的tag
            preTag = ((NavigationActivity) activity).getNavigate().curTag;
        }

        // 为新的activity创建新的navigate
        Navigate navigate = new Navigate(activityTag, preTag);
        navigate.args = params;
        navigate.result = result;
        activity.startActivity(intent);
    }

    /**
     * 界面跳转（出栈方式）
     *
     * @param params 界面结束时要回传给之前界面的参数
     */
    private void pop(Object... params) {
        this.activity.onSupportBackPressed();
        if (params != null && params.length > 0) {
            if (result != null) {
                result.onResult(params);
            }
        }
        removeNavigation(this);
    }

    /**
     * 从activityMap里面清除记录
     *
     * @param navigate
     */
    private void removeNavigation(Navigate navigate) {
        if (!TextUtils.isEmpty(navigate.curTag)) {
            activityMap.remove(navigate.curTag);
        }
    }

    public static void push(Fragment fragment, Class<?> viewCls) {
        pushImpl(fragment.getActivity(), viewCls, null);
    }

    public static void push(Fragment fragment, Class<?> viewCls, Object... params) {
        pushImpl(fragment.getActivity(), viewCls, null, params);
    }

    public static void push(Fragment fragment, Class<?> viewCls, NavigationResult result, Object... params) {
        pushImpl(fragment.getActivity(), viewCls, result, params);
    }

    public static void push(Activity activity, Class<?> viewCls) {
        pushImpl(activity, viewCls, null);
    }

    public static void push(Activity activity, Class<?> viewCls, Object... params) {
        pushImpl(activity, viewCls, null, params);
    }

    public static void push(Activity activity, Class<?> viewCls, NavigationResult result, Object... params) {
        pushImpl(activity, viewCls, result, params);
    }


    public static void pop(Fragment fragment, Object... params) {
        getInstance(fragment).pop(params);
    }

    public static void pop(Context context, Object... params) {
        getInstance(context).pop(params);
    }


    public static void pop(Activity activity, Object... params) {
        getInstance(activity).pop(params);
    }

    /**
     * 两个都是activity的跳转
     *
     * @param activity
     * @param cls
     */
    public static void pushActivity(Activity activity, Class<?> cls) {
        pushActivity(activity, cls, null);
    }

    /**
     * 两个都是activity的跳转
     *
     * @param activity
     * @param cls
     * @param bundle
     */
    public static void pushActivity(Activity activity, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(activity, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        activity.startActivity(intent);
    }


    public static void pushActivity(Fragment fragment, Class<?> cls) {
        pushActivity(fragment.getActivity(), cls);
    }

    public static void pushActivity(Fragment fragment, Class<?> cls, Bundle bundle) {
        pushActivity(fragment.getActivity(), cls, bundle);
    }

    public static void pushActivity(Context context, Class<?> cls) {
        pushActivity(context, cls, null);
    }

    public static void pushActivity(Context context, Class<?> cls, Bundle bundle) {
        if (context instanceof Activity) {
            pushActivity((Activity) context, cls, bundle);
        }
    }

    /**
     * 获取导航栏
     *
     * @return
     */
    public NavigationBar getBar() {
        return bar;
    }
}
